package model

import (
	"common/helper"
	"common/util"
	"errors"
	g "github.com/doug-martin/goqu/v9"
)

type SystemDataSettingParams struct {
	DataKey     string `json:"dataKey" cbor:"dataKey" db:"data_key" goqu:"data_key"`
	DataTitle   string `json:"dataTitle" cbor:"dataTitle" db:"data_title" goqu:"data_title"`
	DataContent string `json:"dataContent" cbor:"dataContent" db:"data_content" goqu:"data_content"`
}
type SystemDataSetting struct {
	Id uint32 `json:"id" cbor:"id" db:"id" goqu:"id"`
	SystemDataSettingParams
}
type SystemDataSettingPageParams struct {
	SystemDataSetting
	util.PageParams
	Data []SystemDataSetting
}
type SystemDataSettingModel struct {
	TableName string
}

func (m SystemDataSettingModel) GetEntityToEx(params SystemDataSetting) g.Expression {
	ex := g.Ex{}
	if params.DataContent != "" {
		ex["data_content"] = params.DataContent
	}
	if params.DataTitle != "" {
		ex["data_title"] = params.DataTitle
	}
	if params.DataKey != "" {
		ex["data_key"] = params.DataKey
	}
	if params.Id != 0 {
		ex["id"] = params.Id
	}
	return ex
}
func (m SystemDataSettingModel) Get(id uint32, ent interface{}) error {
	if id == 0 {
		return errors.New(helper.ParamErr)
	}
	ex := g.Ex{"id": id}
	return m.GetByEx(ex, ent)
}
func (m SystemDataSettingModel) GetByEx(ex g.Expression, ent interface{}) error {
	query, _, _ := dialect.From(m.TableName).Where(ex).Limit(1).ToSQL()
	helper.InfoLog("query sql :{%s}\n", query)
	return meta.MerchantDB.Get(ent, query)
}
func (m SystemDataSettingModel) CountByEx(ex g.Expression) (uint32, error) {
	from := dialect.From(m.TableName).Select(g.COUNT("id"))
	if ex != nil {
		from = from.Where(ex)
	}
	query, _, err := from.ToSQL()
	var total int
	if err = meta.MerchantDB.Get(&total, query); err != nil {
		return 0, err
	}
	return uint32(total), nil
}
func (m SystemDataSettingModel) Count() (uint32, error) {
	return m.CountByEx(nil)
}
func (m SystemDataSettingModel) Page(page *SystemDataSettingPageParams) error {
	return m.PageByEx(page, m.GetEntityToEx(page.SystemDataSetting))
}
func (m SystemDataSettingModel) PageByEx(page *SystemDataSettingPageParams, ex g.Expression) error {
	total, err := m.CountByEx(ex)
	if err != nil {
		return err
	}
	page.Total = total
	currentPage := page.CurrentPage
	if currentPage < 1 {
		currentPage = 1
	}
	offset := (currentPage - 1) * page.PageSize
	if offset < 0 {
		offset = 0
	}
	pageSize := page.PageSize
	if pageSize <= 0 {
		pageSize = 10
	}
	form := dialect.From(m.TableName)
	if ex != nil {
		form = form.Where(ex)
	}
	query, _, _ := form.Offset(uint(offset)).Limit(uint(pageSize)).ToSQL()
	helper.InfoLog("update sql :{%s}\n", query)
	ent := make([]SystemDataSetting, 0)
	err = meta.MerchantDB.Select(&ent, query)
	if err != nil {
		return err
	}
	page.CurrentPage = currentPage
	page.PageSize = pageSize
	page.Data = ent
	return nil
}
func (m SystemDataSettingModel) DeleteById(id uint32) error {
	return m.DeleteByEx(g.Ex{"id": id})
}
func (m SystemDataSettingModel) DeleteByEx(ex g.Expression) error {
	if ex == nil {
		return errors.New(helper.ParamErr)
	}
	query, _, err := dialect.Delete(m.TableName).Where(ex).ToSQL()
	_, err = meta.MerchantDB.Exec(query)
	if err != nil {
		return err
	}
	return nil
}
func (m SystemDataSettingModel) Update(entity SystemDataSetting) error {
	query, _, err := dialect.Update(m.TableName).Set(&entity.SystemDataSettingParams).Where(g.Ex{"id": entity.Id}).ToSQL()
	if err != nil {
		return err
	}
	_, err = meta.MerchantDB.Exec(query)
	if err != nil {
		return err
	}
	return nil
}
func (m SystemDataSettingModel) Insert(entity SystemDataSettingParams) error {
	query, _, err := dialect.Insert(m.TableName).Rows(&entity).ToSQL()
	if err != nil {
		return err
	}
	res, err := meta.MerchantDB.Exec(query)
	_, err = res.LastInsertId()
	if err != nil {
		return err
	}
	return nil
}
